package com.sskj.base;

import android.arch.lifecycle.ViewModelProviders;
import android.os.Bundle;
import android.view.WindowManager;

import com.sskj.base.model.UserViewModel;

import butterknife.ButterKnife;

public abstract class MVPActivity<T extends BasePresenter> extends BaseActivity {

    protected T mPresenter;

    protected String TAG;

    public static UserViewModel userViewModel;

    public static boolean isLogin;

    public static String account;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN | WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        setContentView(getLayoutId());
        ButterKnife.bind(this);
        mPresenter = getPresenter();
        mPresenter.attachView(this);
        TAG = this.getClass().getName();
        getLifecycle().addObserver(mPresenter);
        userViewModel = ViewModelProviders.of(this).get(UserViewModel.class);
        userViewModel.getUser(this).observe(this, user -> {
            if (user != null) {
                isLogin = true;
                account = user.getAccount();
            } else {
                isLogin = false;
                account = "";
            }
        });
        initView();
        initData();
        initEvent();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPresenter.onDestroy();
    }

    public void initEvent() {

    }

    public abstract int getLayoutId();

    public abstract T getPresenter();

    public abstract void initView();

    public abstract void initData();

}
